package mk.edu.ii.navigatemacedonia;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.view.MotionEvent;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Projection;

class MapOverlayPoints extends com.google.android.maps.Overlay
{
	GeoPoint point1;
	GeoPoint point2;
	String siteName;
	public MapOverlayPoints(GeoPoint point1, GeoPoint point2, String siteName){
		this.point1 = point1;
		this.point2 = point2;
		if(siteName.length()>17)
			this.siteName = siteName.substring(0,15)+"...";
		else
			this.siteName = siteName;
		if(siteName.length()<15)
			this.siteName = "    " + siteName;
	}
	
	@Override
	public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {

		super.draw(canvas, mapView, shadow);

		Projection projection = mapView.getProjection();
		// Create and setup your paint brush
		Paint paint = new Paint();
		paint.setARGB(250, 255, 0, 0);
		paint.setAntiAlias(true);
		paint.setFakeBoldText(true);
		if (shadow == false) {
			Point myPoint1 = new Point();
			Point myPoint2 = new Point();
			
			projection.toPixels(point1, myPoint1);
			projection.toPixels(point2, myPoint2);
			
			int rad = 5;
			int length = siteName.length()*8;
			int textSize = 15;
			float angle = 5;
			
			RectF oval1 = new RectF(
					myPoint1.x-rad, myPoint1.y-rad,
					myPoint1.x+rad, myPoint1.y+rad);
			
			Paint backPaint = new Paint();
			backPaint.setARGB(175, 50, 50, 50);
			backPaint.setAntiAlias(true);
			canvas.drawOval(oval1, paint);
			
			RectF oval2 = new RectF(
					myPoint2.x-rad, myPoint2.y-rad,
					myPoint2.x+rad, myPoint2.y+rad);
			
			paint.setARGB(250, 0, 255, 0);
			canvas.drawOval(oval2, paint);
			
			paint.setTypeface(Typeface.MONOSPACE);
			paint.setTextSize(textSize);
			
			if(point1.getLongitudeE6()>point2.getLongitudeE6())
			{
				RectF backRect = new RectF(
						myPoint1.x - 120, 
						myPoint1.y - textSize - 5,
						myPoint1.x - 2 - rad,
						myPoint1.y );
				canvas.drawRoundRect(backRect, angle, angle, backPaint);
				canvas.drawText("You are Here", myPoint1.x - 120, myPoint1.y - 5, paint);
				
				
				backRect = new RectF(
						myPoint2.x + 2 + rad,
						myPoint2.y ,
						myPoint2.x + length,
						myPoint2.y + textSize + 5);
				
				canvas.drawRoundRect(backRect, angle, angle, backPaint);
				
				canvas.drawText(siteName,
						myPoint2.x + 2 + rad - 30 , myPoint2.y+3*rad,
						paint);
			}
			else
			{
				// Device position
				RectF backRect = new RectF(
						myPoint1.x + rad + 2,
						myPoint1.y - textSize - 5,
						myPoint1.x + 120,
						myPoint1.y );
				
				canvas.drawRoundRect(backRect, angle, angle, backPaint);
				canvas.drawText("You are Here",
						myPoint1.x + 2*rad,	myPoint1.y - 5 , paint);
				
				// Destination
				backRect = new RectF(
						myPoint2.x - length - 2,
						myPoint2.y ,
						myPoint2.x - 2 - rad,
						myPoint2.y + textSize + 5);
				
				canvas.drawRoundRect(backRect, angle, angle, backPaint);
				
				canvas.drawText(siteName, 
						myPoint2.x - length - 30, myPoint2.y+3*rad,
						paint);
			}
			
		}
    
		return false;
    }
	@Override
    public boolean onTouchEvent(MotionEvent event, MapView mapView) 
    {   	    	
		return false;
    }
	
	@Override
	public boolean onTap(GeoPoint p, MapView mapView) {
		MapPreviewActivity.startNavigateActivity(point2.getLatitudeE6(), point2.getLongitudeE6());
		return super.onTap(p, mapView);
	}
}
class CompassOverlay extends com.google.android.maps.MyLocationOverlay {
	
	public CompassOverlay(Context context, MapView mapView) {
		super(context, mapView);
		// TODO Auto-generated constructor stub
	}
	@Override
	public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {

		super.draw(canvas, mapView, shadow);
		drawCompass(canvas, 1f);
		return false;
	}
	@Override
    public boolean onTouchEvent(MotionEvent event, MapView mapView) 
    {   	    	
		return false;
    }
}